﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace MyCode
{
    public static class ExtensionString
    {
        public static Int16 ToInt16(this Int16? s)
        {
            return ToInt16(Convert.ToString(s), 0);
        }
        public static Int16 ToInt16(this String s)
        {
            return ToInt16(s, 0);
        }
        public static Int16 ToInt16(this String s, Int16 _defaultValue)
        {
            Int16 r = _defaultValue;
            if (s != null)
            {
                Int16.TryParse(s, out r);
            }
            return r;
        }

        public static Int32 ToInt32(this Int32? s)
        {
            return ToInt32(Convert.ToString(s), 0);
        }
        public static Int32 ToInt32(this String s)
        {
            return ToInt32(s, 0);
        }
        public static Int32 ToInt32(this String s, Int32 _defaultValue)
        {
            int r = _defaultValue;
            if (s != null)
            {
                Int32.TryParse(s, out r);
            }
            return r;
        }
        public static Int64 ToInt32(this Int64? s)
        {
            return ToInt64(Convert.ToString(s), 0);
        }
        public static Int64 ToInt64(this String s)
        {
            return ToInt64(s, 0);
        }
        public static Int64 ToInt64(this String s, Int64 _defaultValue)
        {
            Int64 r = _defaultValue;
            if (s != null)
            {
                Int64.TryParse(s, out r);
            }
            return r;
        }
        public static Double ToDouble(this Double? s)
        {
            return ToDouble(Convert.ToString(s), 0.0);
        }
        public static Double ToDouble(this String s)
        {
            return ToDouble(s, 0.0);
        }
        public static Double ToDouble(this String s, Double _defaultValue)
        {
            Double r = _defaultValue;
            if (s != null)
            {
                Double.TryParse(s, out r);
            }
            return r;
        }
        public static Decimal ToDecimal(this Decimal? s)
        {
            return ToDecimal(Convert.ToString(s), 0.0m);
        }
        public static Decimal ToDecimal(this String s)
        {
            return ToDecimal(s, 0.0m);
        }
        public static Decimal ToDecimal(this String s, Decimal _defaultValue)
        {
            Decimal r = _defaultValue;
            if (s != null)
            {
                Decimal.TryParse(s, out r);
            }
            return r;
        }

        public static DateTime? ToDateTime(this String s)
        {
            DateTime dt = DateTime.Now;
            if (DateTime.TryParse(s, out dt) == false)
            {
                return null;
            }
            return dt;
        }
        public static string ToShoutDateTime(this DateTime? dt)
        {
            if (dt == null)
            {
                return "";
            }
            return ToShoutDateTime(Convert.ToDateTime(dt));
        }
        public static string ToShoutDateTime(this DateTime dt)
        {
            DateTime tempdt = dt;
            if (tempdt != null)
            {
                return String.Format("{0:yyyy-MM-dd}", Convert.ToDateTime(tempdt));
            }
            else
            {
                return "";
            }
        }
        public static Boolean ToBoolean(this Boolean? s)
        {
            if (s == null)
            {
                return false;
            }
            else
            {
                return ToBoolean(Convert.ToString(s));
            }
        }
        public static Boolean ToBoolean(this String s)
        {
            if (String.IsNullOrEmpty(s) == false && (s == "1" || Convert.ToString(s).ToLower() == "true"))
            {
                return true;
            }
            return false;
        }
        public static Char ToChar(this String s)
        {
            return ToChar(s, ' ');
        }
        public static Char ToChar(this String s, Char _defaultValue)
        {
            Char r = _defaultValue;
            if (s != null)
            {
                Char.TryParse(s, out r);
            }
            return r;
        }
        public static Boolean IsValidEmail(this String s)
        {
            Regex rCode = new Regex(@"^\w+((-\w+)|(\.\w+))*\@\w+((\.|-)\w+)*\.\w+$");
            if (rCode.IsMatch(s) == false)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        public static Boolean IsDigit(this String s)
        {
            Regex rCode = new Regex(@"^\d*$");
            if (string.IsNullOrEmpty(s) == true || rCode.IsMatch(s.Trim()) == false)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        public static string TrimPrefix(this string sourceString, string prefix, bool ignoreCase = false)
        {
            prefix = prefix ?? string.Empty;
            if (!sourceString.StartsWith(prefix, ignoreCase, System.Globalization.CultureInfo.CurrentCulture))
            {
                return sourceString;
            }

            return sourceString.Remove(0, prefix.Length);
        }

        public static string TrimSuffix(this string sourceString, string suffix, bool ignoreCase = false)
        {
            suffix = suffix ?? string.Empty;
            if (!sourceString.EndsWith(suffix, ignoreCase, System.Globalization.CultureInfo.CurrentCulture))
            {
                return sourceString;
            }
            return sourceString.Substring(0, sourceString.Length - suffix.Length);
        }
        /// <summary>
        /// 公历转为农历的函数
        /// </summary>
        /// <param name="solarDateTime">公历日期</param>
        /// <returns>农历的日期</returns>
        public static string SolarToChineseLunisolarDate(DateTime solarDateTime)
        {
            System.Globalization.ChineseLunisolarCalendar cal = new System.Globalization.ChineseLunisolarCalendar();

            int year = cal.GetYear(solarDateTime);
            int month = cal.GetMonth(solarDateTime);
            int day = cal.GetDayOfMonth(solarDateTime);
            int leapMonth = cal.GetLeapMonth(year);
            return string.Format("农历{0}{1}（{2}）年{3}{4}月{5}{6}"
                                , "甲乙丙丁戊己庚辛壬癸"[(year - 4) % 10]
                                , "子丑寅卯辰巳午未申酉戌亥"[(year - 4) % 12]
                                , "鼠牛虎兔龙蛇马羊猴鸡狗猪"[(year - 4) % 12]
                                , month == leapMonth ? "闰" : ""
                                , "无正二三四五六七八九十冬腊"[leapMonth > 0 && leapMonth <= month ? month - 1 : month]
                                , "初十廿三"[day / 10]
                                , "日一二三四五六七八九"[day % 10]
                                );
        }
    }
}